<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>英雄管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-table :data="articleList" style="width: 100%" stripe border>
        <el-table-column type="index" label="#" width="width"></el-table-column>
        <el-table-column prop="_id" label="ID" width="width"></el-table-column>
        <el-table-column label="文章标题" width="width" prop="title"></el-table-column>

        <el-table-column label="操作" width="width">
          <template slot-scope="scope">
            <el-button
              type="success"
              size="small"
              icon="el-icon-edit"
              @click="editArticle(scope.row)"
            >编辑</el-button>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="removeArticle(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'EquipList',
  data() {
    return {
      articleList: []
    }
  },
  methods: {
    // 获取列表数据
    async getArticleList() {
      const { data: res } = await this.$http.get('rest/articles')
      console.log(res)
      this.articleList = res
    },

    editArticle(row) {
      this.$message.info('即将跳转到编辑界面')
      this.$router.push(`/articles/edit/${row._id}`)
    },

    // 删除
    async removeArticle(row) {
      const confirm = await this.$confirm(
        `是否删除文章:${row.title}`,
        '删除操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirm === 'confirm') {
        await this.$http.delete(`rest/articles/${row._id}`)
        this.$message.success(`成功删除文章：${row.title}`)
        this.getArticleList()
      } else {
        this.$message.info('取消删除操作')
      }
    }
  },
  beforeEach(to, from, next) {
    if (from.path === '/articles/list') {
      this.parentName.splice(0, this.parentName.length)
      next()
    }
  },
  beforeDestroy() {
    this.parentName = this.$options.data.parentName
  },
  created() {
    this.getArticleList()
  }
}
</script>